package com.xc.freeapp.account.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.xc.freeapp.bean.AuthInfo;
import com.xc.freeapp.common.ResponseBean;
import com.xc.freeapp.entity.Vaccine;
import com.xc.freeapp.exception.BaseException;
import com.xc.freeapp.service.VaccineService;
import com.xc.freeapp.util.AuthUtil;
import com.xc.freeapp.util.Constant;
import com.xc.freeapp.util.DateUtils;
import com.xc.freeapp.util.JsonUtil;
import com.xc.freeapp.util.TokenAnnotation;
@Controller
@RequestMapping("/vaccine")
@Api(value="/vaccine",description = "疫苗")
public class VaccineController extends AbstractController {
	@Autowired
	private VaccineService vaccineServiceImpl;
	/**
	 * 获取疫苗信息
	 * 
	 * @param requestMap
	 * @return
	 */
	@TokenAnnotation(required=false)
	@RequestMapping(value = "{vaccineperid}", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "获取疫苗信息")
	public List<Vaccine> getVaccineList(@PathVariable("vaccineperid") String vaccineperid)
			throws BaseException {
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		try{
			Map<String, String> map = new HashMap<String, String>();
		
			map.put("userid", authInfo.getUserId());
			map.put("businessId", authInfo.getAppId());
			map.put("registerType", authInfo.getPlatform());
			map.put("vaccineperid", vaccineperid);
			List<Vaccine> vaccines = vaccineServiceImpl.getVaccineList(map);
			return vaccines;
		} catch (BaseException e) {
			log.error(":获取疫苗列表" + e.getMessage());
			throw e;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error(":获取疫苗列表" + e.getMessage());
			throw new BaseException(Constant.GLOBAL_ERROR);
		}
	}

	/**
	 * 更新疫苗信息
	 * 
	 * @param requestMap
	 * @return
	 */
	@TokenAnnotation(required=true)
	@RequestMapping(value = "", method = RequestMethod.PUT)
	@ResponseBody
	public ResponseBean saveVaccine(@RequestBody List<Vaccine> vaccines)throws BaseException {
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		try {
			for (Vaccine vaccine:vaccines) {
				vaccine.setCreateby(authInfo.getUserIntId());
				vaccine.setUpdatetime(DateUtils.getDate());
				vaccineServiceImpl.saveVaccine(vaccine);
			}
			return ResponseBean.getSuccess();
		} catch (BaseException e) {
			log.error(":更新疫苗信息出错" + e.getMessage());
			throw e;
		} catch (Exception e) {
			log.error(":更新疫苗信息出错" + e.getMessage());
			throw new BaseException(Constant.GLOBAL_ERROR);
		}
	}


}
